package com.stedu.controller;

import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.stedu.bean.Admin;
import com.stedu.bean.RespBean;
import com.stedu.exception.SteduException;
import com.stedu.service.AdminService;
import com.stedu.utils.JwtUtil;
import com.stedu.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RedisUtil redisUtil;


    @PutMapping("/admin/updateByOid")
    public RespBean updateByOid(@RequestBody Admin admin) throws SteduException {
        String md5pwd = SecureUtil.md5(SecureUtil.md5(admin.getPassword()));
        admin.setPassword(md5pwd);
        if (adminService.updateByOid(admin)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }
    @PutMapping("/admin/updateByPid")
    public RespBean updateByPid(@RequestBody Admin admin) throws SteduException {
        String md5pwd = SecureUtil.md5(SecureUtil.md5(admin.getPassword()));
        admin.setPassword(md5pwd);
        if (adminService.updateByPid(admin)) {
            return RespBean.ok("修改成功~~~");
        } else {
            return RespBean.error("修改失败，请重新修改");
        }
    }
    @DeleteMapping("/admin/deleteByOid/{oid}")
    public RespBean deleteByOid(@PathVariable("oid")Integer oid) throws SteduException {
        if (adminService.deleteByOid(oid)) {
            return RespBean.ok("删除成功~~~");
        } else {
            return RespBean.error("删除失败，请重新删除");
        }
    }
    @DeleteMapping("/admin/deleteByPid/{pid}")
    public RespBean deleteByPid(@PathVariable("pid")Integer pid) throws SteduException {
        if (adminService.deleteByPid(pid)) {
            return RespBean.ok("删除成功~~~");
        } else {
            return RespBean.error("删除失败，请重新删除");
        }
    }
    @PostMapping("/admin/insert")
    public RespBean insert(@RequestBody Admin admin) throws SteduException {
        //添加
        String md5pwd = SecureUtil.md5(SecureUtil.md5(admin.getPassword()));
        admin.setPassword(md5pwd);
        if (adminService.insert(admin)) {
            return RespBean.ok("添加成功~~~");
        } else {
            return RespBean.error("添加失败，请重新添加");
        }
    }
    //权限验证
    @GetMapping("/admin/select")
    public RespBean select(@RequestHeader("token") String token){
            // 处理
        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
                return RespBean.ok("欢迎人员",map);

    }


    //登录
    @ResponseBody
    @PostMapping("/login")
    public RespBean login(String username, String password, String captchaId,String captcha ) throws SteduException {
        //判断验证码，和redis中的验证码对比
        String captchaRedis =(String)redisUtil.get(captchaId);
        if(captchaRedis != null && !captchaRedis.equalsIgnoreCase(captcha)){
            return RespBean.error("验证码错误,请刷新后重新输入");
        }
        //判断用户名和密码是否正确
        Admin admin = adminService.login(username, password);
        //登录之后发送JWT给前端
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", admin.getId());
        map.put("username", admin.getUsername());
        map.put("oid", admin.getOid());
        map.put("pid",admin.getPid());
        map.put("root",admin.getRoot());
        String jwt = JwtUtil.generateJwt(map);
        return RespBean.ok("登陆成功",jwt);
    }

    //注销
    @ResponseBody
    @GetMapping("/logout")
    public RespBean logout(HttpSession session) {
        //让session失效
        session.invalidate();

        //返回注销成功的消息
        return RespBean.ok("退出成功");
    }

    //验证码
    @GetMapping("/captcha")
    public RespBean captcha() throws IOException{
        LineCaptcha captcha = new LineCaptcha(120,38,4,20);
        String code = captcha.getCode();
        String captchaId = IdUtil.getSnowflakeNextIdStr();
        redisUtil.set(captchaId,code,90);
        //Base64编码
        String captchaImageBase64Data = captcha.getImageBase64Data();
        HashMap<String,Object>map = new HashMap<>();
        map.put("captchaId", captchaId);
        map.put("captchaImageBase64Data",captchaImageBase64Data);

        return  RespBean.ok("", map);
    }
}
